package academic.ui.client.widgets;

import ui.client.GenericContainerTag;

import academic.ui.client.widgets.Flip;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class QuickFlipWidget extends Composite  {

	GenericContainerTag frontDiv = new GenericContainerTag("div");
	GenericContainerTag backDiv = new GenericContainerTag("div");
	String styleName; 
	public QuickFlipWidget(String styleName) {
		this.styleName=styleName;
		GenericContainerTag mainDiv = new GenericContainerTag("div");
		mainDiv.setStyleName(styleName);
		frontDiv.setStyleName("redPanel");
		backDiv.setStyleName("blackPanel");
		mainDiv.add(frontDiv);
		mainDiv.add(backDiv);
	
	
		initWidget(mainDiv);
	}
	
	public void setContent(GenericContainerTag front,GenericContainerTag back) {
		frontDiv.setVisible(true);
		frontDiv.clear();
		backDiv.clear();
		GenericContainerTag contentDiv = new GenericContainerTag("div");
		if(front!=null)
			contentDiv.add(front);
		frontDiv.add(contentDiv);
		contentDiv.setStyleName("buttonContent");
		GenericContainerTag contentDivBack = new GenericContainerTag("div");
		contentDivBack.setStyleName("buttonContent");
		if(back!=null) {
			backDiv.add(contentDivBack);
			contentDivBack.add(back);
		}
	}
	
	public void flip() {
		Flip.doFlip(styleName);
	}
	
	public void updateFront(GenericContainerTag front) {
		frontDiv.clear();
		GenericContainerTag contentDiv = new GenericContainerTag("div");
		contentDiv.add(front);
		frontDiv.add(contentDiv);
		contentDiv.setStyleName("buttonContent");
	}
	
	public void updateBack(Widget back) {
		getElement().setId(back.getElement().getId());
		backDiv.clear();
		GenericContainerTag contentDiv = new GenericContainerTag("div");
		contentDiv.add(back);
		backDiv.add(contentDiv);
		contentDiv.setStyleName("buttonContent");
	}
	
	public Boolean isDefaultShowing() {
		return !DOM.getStyleAttribute(frontDiv.getElement(), "display").equals("none");
	}
	
	public void showDefault() {
		DOM.setStyleAttribute(getElement(),"display","inline-block");
		DOM.setStyleAttribute(frontDiv.getElement(), "display", "block");
		DOM.setStyleAttribute(backDiv.getElement(), "display", "none");
	}
	
	public Boolean hasBackWidget() {
		return backDiv.getWidgetCount()>0;
	}
	
	public void flip(int i) {
		Flip.doFlipToPanel(styleName,i);
	}

	public void hideFront() {
		DOM.setStyleAttribute(frontDiv.getElement(), "display", "none");

	}
}
